package at.fhooe.im440.pandora.entities;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import at.fhooe.im440.pandora.collision.CircleCollider;
import at.fhooe.im440.pandora.core.Core;

public class AsteroidEntity extends Entity {
	
	private static final double PI = Math.PI;
	private int screenXRight;
	private int	screenXLeft;
	private int screenYUp;
	private int screenYDown;
	
	private float height;
	private float width;
	private float diagonale;

	private float x;
	private float y;
	private float driftDirection;
	private float rotation;
	private float spin;
	private float speed;
	
	private int maxsize;
	private int minsize;
	private float spin_max;
	private float min_speed;
	private float max_speed;
	
	private boolean m_alive = true;
	
	
	public AsteroidEntity(String name, Core core, EmmitterSettings es) {
		super(name, core, -1000, -1000, 0);
		
		max_speed = es.getMax_speed();
		minsize = es.getMinsize();
		maxsize = es.getMaxsize();
		spin_max = es.getSpin_max();
		min_speed = es.getMin_speed();
		max_speed = es.getMax_speed();
		
		rotation = (float) (Math.random()*(PI*2));
		setRandomSize();
		//setRandomSpeed();
		speed = 1.0f;
		setUpScreenEdge();
		setUpPosition();
		calculateRandomPivotAndSpin();
		core.collisionTester.addCollisionObject(super.getCollisionObject());
	}

	@SuppressWarnings("unused")
	private void setRandomSpeed() {
		speed = (float) (Math.random()*(max_speed-min_speed) + min_speed);
	}

	private void calculateRandomPivotAndSpin() {
		spin = (float) (Math.random()*(spin_max)) - spin_max/2;
	}

	private void setRandomSize() {
		height = (int) (minsize + Math.random()*maxsize);
		width = (int) (minsize + Math.random()*maxsize);
		diagonale = (float) Math.sqrt(Math.pow(height, 2) + Math.pow(width, 2));
	}

	/**
	 * randomly creats Asteroid on Screen-Edge and let it float towards screen 
	 */
	private void setUpPosition() {
		int scrWidth = core.getGraphicsConfig().getWidth();
		int scrHeight = core.getGraphicsConfig().getHeight();
		int side = (int)(Math.random()*4);

		switch(side){
		//left edge
		case 0:
//			System.out.println("Asteroid von links");
			x = screenXLeft - diagonale;
			y = (float) ((Math.random()* scrHeight) - scrHeight/2);
			driftDirection = (float) ((Math.random()*PI) - (PI/2));
			break;
		//lower edge
		case 1:
//			System.out.println("Asteroid von unten");
			x = (float) ((Math.random()*scrWidth) - scrWidth/2);
			y = -(scrHeight/2 + diagonale);
			driftDirection = (float) ((Math.random()*PI));
			break;
		//right edge	
		case 2:
//			System.out.println("Asteroid von rechts");
			x = scrWidth/2 + diagonale;
			y = (float) ((Math.random()*scrHeight) - scrHeight/2);
			driftDirection = (float) ((Math.random()*PI) + (PI/2));
			break;
		//upper edge
		case 3:
//			System.out.println("Asteroid von oben");
			x = (float) ((Math.random()*scrWidth) - scrWidth/2);
			y = scrHeight/2 + diagonale;
			driftDirection = (float) ((Math.random()*PI) + PI);
			break;
		}
		
	}

	private void setUpScreenEdge() {
		screenXRight = core.getGraphicsConfig().getWidth() /2;
		screenXLeft = screenXRight *-1;
		screenYUp = core.getGraphicsConfig().getHeight() /2;
		screenYDown = screenYUp *-1;
	}

	@Override
	public void update() {
		if(isAlive() && m_alive){
			y += speed * Math.sin(driftDirection);
			x += speed * Math.cos(driftDirection);
			if(width > height)
				super.updateCollider(x+width/2.0f, y+height/2.0f, width);
			else
				super.updateCollider(x+width/2.0f, y+height/2.0f, height);
			rotation = (float) ((rotation + spin) % (2 *PI));
		} else {
			core.collisionTester.removeCollisionObject(getCollisionObject());
			core.entityManager.removeEntity(this);
			//tearDown();
		}			

		
	}

	private boolean isAlive() {
		return !(x < (screenXLeft - maxsize*2) || x > (screenXRight + maxsize*2) || 
				y < (screenYDown - maxsize*2) || y > (screenYUp + maxsize*2));
	}

	@Override
	public Point2D getPosition() {
		return new Point2D.Float(x, y);
	}

	@Override
	public void setPosition(float x, float y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public void render(Graphics2D g) {
		g.setColor(new Color(200, 200, 200));
		g.rotate(rotation, x, y);
		g.fillRect ((int)x, (int)y, (int)width, (int)height);
		if(core.isDebug)
		{
			super.drawCollider(g);
		}
		g.rotate(-rotation, x, y);
		/*
		g.setColor(new Color(200, 200, 200));
		g.rotate(rotation, x+randPivotX, y+randPivotY);
		g.fillRect ((int)x, (int)y, (int)width, (int)height);
		if(core.isDebug)
		{
			super.drawCollider(g);
		}
		g.rotate(-rotation, x+randPivotX, y+randPivotY);
		*/
	}

	//CircleCollider
	@Override
	public void collided(CircleCollider col) {
		m_alive = false;
	}
}
